package com.gxa.controller;

import cn.hutool.db.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.common.base.R;
import com.gxa.common.base.RespResult;
import com.gxa.common.base.RespResultCode;
import com.gxa.domain.dto.PageDto;
import com.gxa.domain.dto.UserDto;
import com.gxa.domain.entity.User;
import com.gxa.domain.entity.Warehouse;
import com.gxa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 万事顺意
 * Created with IntelliJ IDEA.
 * User: zwj
 * Date: 2024/07/1611:02
 * Description:
 */
@RestController
@RequestMapping("/logistics_staff")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")

    public RespResult login(@RequestBody UserDto dto) {
        User login = userService.login(dto.getPhone(), dto.getPassword());
        if (login != null) {
            return RespResult.success(login);
        }
        return RespResult.error("登陆失败");
    }
    @PostMapping("/register")
    public RespResult register(@RequestBody User user){
        User register= userService.register(user);
        return RespResult.success(register);
    }
    /**
     *员工管理查询功能
     */
    @GetMapping("/query")
    public RespResult query(Integer id){
        User byId= userService.getById(id);
        return RespResult.success(byId);
    }
    @PostMapping("/save")
    public RespResult save(@RequestBody User user){
        boolean save= userService.save(user);
        if (save){
            return RespResult.success(save);
        }
        return RespResult.error("添加失败");
    }
    @PostMapping("/update")
    public RespResult update(User user){
        boolean b= userService.updateById(user);
        if (b){
            return RespResult.success(b);
        }
        return RespResult.error("修改失败");
    }
    @PostMapping("/delete")
    public RespResult delete(Integer id){
        boolean b= userService.removeById(id);
        if (b){
            return RespResult.success(b);
        }
        return RespResult.error("删除失败");
    }
    @GetMapping("/list")
    public RespResult list1(){
        List<User> list= userService.list();
        return RespResult.success(list);
    }
    //分页查询
    @PostMapping("/search")
    public RespResult search(PageDto dto) {
        Page<User> page = new Page<>(dto.getPage(), dto.getLimit());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (dto.getUser() != null && !dto.getUser().isEmpty()) {
            queryWrapper.like("user", dto.getUser());
        }
        Page<User> result = userService.page(page, queryWrapper);
        // 获取总记录数
        long total = result.getTotal();
        // 获取分页数据
        List<User> records = result.getRecords();
        return RespResult.success(records,total);
    }
}
